<?php
function subaccount_del ($gametype)
{
    $id = intval($_GET['id']);
    $rs = getdb("select * from k_user where acl=-4 and id=" . $id);
    if (isset($_GET['ok']))
    {
        getdb("delete from k_user where acl=-4 and id=" . $id);
        echo "<p>删除帐号成功！</p>";
    }
    else
    {
        echo "<p>是否确定删除这个子帐号？<b>" . $rs->f['account'] . "</b></p>";
        echo "<p>[ &nbsp; ";
        echo "<a href=op.php?op=agent&fp=subaccount&gametype=" . $gametype . "&cmd=del&id=" . $id . "&ok=1>是</a>";
        echo " &nbsp; | &nbsp; <a href=op.php?op=agent&fp=subaccount&gametype=" . $gametype . ">否</a> &nbsp; ]</p>";
    }
}

function subaccount_list ($gametype)
{
    global $curruser;
    echo "<table border=1 cellpadding=3 cellspacing=0>";
    echo "<tr class=\"title0  bgImg_{$gametype}\"><td>帐号</td><td>名称</td><td>功能</td></tr>";
    $rs = getdb("select * from k_user where acl=-4 and prev_id=" . $curruser['id']);
    while (!$rs->eof)
    {
        echo "<tr>";
        echo "<td>" . $rs->f['account'] . "</td>";
        echo "<td>" . $rs->f['name'] . "</td>";
        echo "<td>";
        echo " [ <a href=op.php?op=agent&fp=subaccount&gametype=" . $gametype . "&cmd=edit&id=" . $rs->f['id'] . ">修改</a>";
        echo " | <a href=op.php?op=agent&fp=subaccount&gametype=" . $gametype . "&cmd=del&id=" . $rs->f['id'] . ">删除</a>";
        echo " ] </td>";
        echo "</tr>";
        $rs->movenext();
    }
    echo "</table>";
}

function subaccount_add ($gametype)
{
    global $curruser;
    $err = "";
    if (isset($_POST['account']))
    {
        $account = getvar("account");
        $pass = getvar("pass");
        if ($account == "")
        {
            $err .= "请输入帐号！\\n";
        }
        if ($pass == "")
        {
            $err .= "请输入密码！\\n";
        }
        if ($err == "")
        {
            $rs = getdb("select * from k_user where account='" . $account . "'");
            if (!$rs->eof)
            {
                $err .= "帐号已经存在，请用其他的帐号！\\n";
            }
        }
        if ($err == "")
        {
            $name = getvar("name");
            if ($name == "")
            {
                $name = $account;
            }
            $sql = "insert into k_user (account,name,pass,acl,enabled,prev_id) values (" . sqlstr($account) . "," . sqlstr($name) . ",'" . md5($pass) . "',-4,1," . $curruser['id'] . ")";
            getdb($sql);
            echo "<p>新增帐号成功！</p>";
            echo "<p>帐号：<b>{$account}</b><br>";
            echo "密码：<b>{$pass}</b></p>";
        }
    }
    else
    {
        echo "<form action=\"op.php?op=agent&fp=subaccount&gametype=" . $gametype . "&cmd=add\" method=post onsubmit=\"if (this.account.value=='') {\r\n            alert('请输入帐号！');\r\n            this.account.focus();\r\n            return false;\r\n        } else if (this.account.value.length < 3) {\r\n            alert('帐号至少要 3 码！');\r\n            this.account.focus();\r\n            return false;\r\n        }\r\n        if (this.pass.value=='') {\r\n            alert('请输入密码！');\r\n            this.pass.focus();\r\n            return false;\r\n        } else if (this.pass.value.length < 4) {\r\n            alert('密码至少要 4 码！');\r\n            this.pass.focus();\r\n            return false;\r\n        }\r\n        if (this.pass.value != this.pass2.value) {\r\n            alert('两个密码不一样，请检查！');\r\n            this.pass.focus();\r\n            return false;\r\n        }\">";
        echo "<table border=0 cellpadding=2 cellspacing=0>";
        echo "<tr><td align=right>帐号：</td><td><input name=account size=20 maxlength=25> (最少3码)</td></tr>";
        echo "<tr><td align=right>名称：</td><td><input name=name size=20 maxlength=25></td></tr>";
        echo "<tr><td align=right>密码：</td><td><input type=password name=pass size=20 maxlength=25> (最少 4 码)</td></tr>";
        echo "<tr><td align=right>再次输入密码：</td><td><input type=password name=pass2 size=20 maxlength=25></td></tr>";
        echo "</table>";
        echo "<input type=submit value=新增>";
        echo "</form>";
        if ($err != "")
        {
            echo "<script>alert(\"" . $err . "\");</script>";
        }
    }
}

function subaccount_edit ($gametype)
{
    global $curruser;
    $id = intval($_GET['id']);
    $rs = getdb("select * from k_user where acl=-4 and id=" . $id);
    if ($rs->eof)
    {
        echo "<p class=error>错误的帐号 ID!</p>";
    }
    else
    {
        $err = "";
        if (isset($_POST['pass']))
        {
            $f = array();
            if (getvar("pass") != "")
            {
                $f['pass'] = "'" . md5(getvar("pass")) . "'";
            }
            $f['name'] = "'" . getvar("name") . "'";
            db_update("k_user", $f, "acl=-4 and id=" . $id);
            echo "<p>修改帐号成功！</p>";
        }
        else
        {
            echo "<form action=\"op.php?op=agent&fp=subaccount&gametype=" . $gametype . "&cmd=edit&id=" . $id . "\" method=post onsubmit=\"if (this.pass.value!='') {\r\n            if (this.pass.value.length < 4) {\r\n                alert('密码至少要 4 码！');\r\n                this.pass.focus();\r\n                return false;\r\n            }\r\n            if (this.pass.value != this.pass2.value) {\r\n                alert('两个密码不一样，请检查！');\r\n                this.pass.focus();\r\n                return false;\r\n            }\r\n        }\">";
            echo "<table border=0 cellpadding=2 cellspacing=0>";
            echo "<tr><td align=right>帐号：</td><td>" . $rs->f['account'] . "</td></tr>";
            echo "<tr><td align=right>名称：</td><td><input name=name value=\"" . $rs->f['name'] . "\" size=20 maxlength=25></td></tr>";
            echo "<tr><td align=right>密码：</td><td><input type=password name=pass size=20 maxlength=25> (最少 4 码)</td></tr>";
            echo "<tr><td align=right>再次输入密码：</td><td><input type=password name=pass2 size=20 maxlength=25></td></tr>";
            echo "</table>";
            echo "<input type=submit value=修改>";
            echo "</form>";
            if ($err != "")
            {
                echo "<script>alert(\"" . $err . "\");</script>";
            }
        }
    }
}

if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
$gametype = intval(getvar("gametype"));
$gType = chkGametype($gametype);
switch ($gType)
{
    case 0 :
        $ui = new UI();
        break;
    default :
        include_once( "class_3d/CheckMode_3d.php" );
        $chk = new CheckMode_3d($curruser['acl'], 1, $gametype);
        $chk->checkShow3dPage();
        unset($chk);
        $ui = new UI_3D();
        $jslink = "\r\n    <script language=\"JScript.Encode\" src=\"site.js\"></script>\r\n    <script language=\"VBScript.Encode\" src=\"site.vbs\"></script>";
        $ui->set("jslink", $jslink);
        $ui->header();
        $ui->showNews3D($curruser['acl']);
        break;
}
$cmd = "list";
if (isset($_GET['cmd']))
{
    $cmd = $_GET['cmd'];
}
$subacl = 0 - 4;
echo title("子帐号管理", "<a href=op.php?op=agent&fp=subaccount&gametype=" . $gametype . "&cmd=list>列表</a>" . " &nbsp;&nbsp; <a href=op.php?op=agent&fp=subaccount&gametype=" . $gametype . "&cmd=add>新增</a>");
echo "<br>";
switch ($cmd)
{
    case "edit" :
        subaccount_edit($gametype);
        break;
    case "add" :
        subaccount_add($gametype);
        break;
    case "del" :
        subaccount_del($gametype);
        break;
    case "list" :
        subaccount_list($gametype);
        break;
}
$ui->footer();
?>